Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
Problem using import/export in Javascript "Uncaught SyntaxError: Cannot use import statement outside a module"

I'm learning Javascript and I have a problem using import/export to modularize my code. I tried to learn this by reading this and this in MDN Web Docs (Mozilla), but failed. I know this has already been asked here, but I couldn't fix the problem.

The error I get in the web browser terminal is: Uncaught SyntaxError: Cannot use import statement outside a module

I'll insert a small example of how I tried to use import/export:

index.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8'>
    </head>
    <body>
        <h1 class="title">Example</h1>
        <button class="title-button">Change title</button>
        <!--
            I tried adding this line but nothing has changed:
            <script type="module" src="module.js"></script>
        --->
        <script src='main.js'></script>
    </body>
</html>

module.js:

const changeTitle = newTitle => {
    const title = document.querySelector(".title");
    
    title.innerHTML = newTitle;
}

export { changeTitle };

main.js:

import { changeTitle } from "./module";

const titleButton = document.querySelector(".title-button");

titleButton.addEventListener("click", () => {
    let newTitle = prompt("Enter new title:");
    
    changeTitle(newTitle);
});

Note: all three files are in the same folder.

Thank you for your time. Sorry if I made a mistake in this post.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The type="module" attribute must be added in the script that uses the import statement (in this case, main.js). My mistake was trying to add type="module" in module.js instead of main.js

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda